const CDBSave = require('./dbsave');


class Task extends CDBSave.CDBSave{
    constructor(){
        super();
    }

    filePath(){
        return 'cd_task.json'
    }


    load(data){
        if(!data){
            return
        }
        this.notices = data;
    }

    save(){
        return this.notices;
    }

    getInfo(){
        return this.notices;
        
    }

    AddTask(User_address,express){
        let id = this.notices.length + 1;
        User_address.id = id;
        let message = Object.assign(User_address,express);  //将两个对象合并成一个对象
        let Add_Task = new Task_child(message);
        this.notices.push(Add_Task)
        return this.notices;
    }

    paoshou_OrderReceiving(id,number){
        let flag = 0;
        for(let i=0;i<this.notices.length;i++){
            if(this.notices[i].id == id && this.notices[i].publisher_number != number){
                flag = 1;
                this.notices[i].select_paoshou = number;
                break;
            }else{
                continue;
            }
        }

        if(flag == 0){
            return -1;
        }else{
            return 0;
        }
    }

    remove_Task(index){
        let flag = 0;
        for(let i=0;i<this.notices.length;i++){
            if(this.notices[i].id == index && this.notices[i].select_paoshou == ''){
                this.notices.splice(i,1);
                flag = 1;
            }
        }

        if(flag == 1){
            for(let i=0;i<this.notices.length;i++){
                this.notices[i].id = i+1;
            }
            return 0;
        }else{
            return -1;
        }
    }


    //搜索功能
    Search_message(message){
        let Arr = new Array();
        for(let i=0;i<this.notices.length;i++){
            let reg = new RegExp(message,"g");
            if(reg.test(this.notices[i].take_item) || reg.test(this.notices[i].give_item) || reg.test(this.notices[i].type)){
                // console.log(this.notices[i]);
                Arr.push(this.notices[i]);
            }
        }
        return Arr;
    }


    //跑手完成任务功能
    Finished_Task(id,number){
        for(let i=0; i<this.notices.length; i++){
            if(this.notices[i].id == id && this.notices[i].publisher_number == number){
                this.notices[i].whether_finished = 1;
                this.notices[i].whether_finished_paoshou = 1;
                break;
            }
        }
    }

    //用户确定收货
    Finished_Task_order(index){
        for(let i=0;i<this.notices.length;i++){
            if(this.notices[i].id == index && this.notices[i].whether_finished_order == 0){
                this.notices[i].whether_finished_order = 1;
                break;
            }
        }
    }

}


class Task_child{
    constructor(message){
        this.load(message)
    }

    load(data){
        this.id = data.id;
        this.name = data.name;
        this.size = data.size;
        this.take_item = data.Address;
        this.type = data.type;
        this.give_item = data.address;
        this.telephone = data.telephone_number;
        this.get_code = data.pick_up_code;
        this.publisher_number = data.number;
        this.Notes = data.Notes;
        this.select_paoshou = "";
        this.position = data.position;
        this.give_position = data.give_position;
        this.whether_finished_paoshou = 0;
        this.whether_finished_order = 0;
    }

    save(){
        return{
            id:this.id,
            name:this.name,
            size:this.size,
            take_item:this.take_item, 
            type:this.type,
            give_item:this.give_item,
            telephone:this.telephone,
            get_code:this.get_code,
            publisher_number:this.publisher_number,
            Notes:this.Notes,
            select_paoshou:this.select_paoshou,
            position:this.position,
            give_position:this.give_position,
            whether_finished_paoshou:this.whether_finished_paoshou,
            whether_finished_order:this.whether_finished_order
        }
    }
}

module.exports = {Task,Task_child}